package com.scenery;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.GeoPoint;
import com.baidu.mapapi.ItemizedOverlay;
import com.baidu.mapapi.ItemizedOverlay.OnFocusChangeListener;
import com.baidu.mapapi.MKAddrInfo;
import com.baidu.mapapi.MKDrivingRouteResult;
import com.baidu.mapapi.MKPoiInfo;
import com.baidu.mapapi.MKPoiResult;
import com.baidu.mapapi.MKSearch;
import com.baidu.mapapi.MKSearchListener;
import com.baidu.mapapi.MKTransitRouteResult;
import com.baidu.mapapi.MKWalkingRouteResult;
import com.baidu.mapapi.MapActivity;
import com.baidu.mapapi.MapView;
import com.baidu.mapapi.OverlayItem;
import com.baidu.mapapi.PoiOverlay;
import com.scenery.vo.Scenery;
import com.scenery.vo.User;
/***
 * 景点周边搜索
 * ***/
public class SearchNearActivity extends MapActivity {
	MapView mMapView = null; // 地图View
	MKSearch mSearch = null; // 搜索模块，也可去掉地图模块独立使用

	private String tag = UserFriendMapActivity.class.getName();
	SeekBar fanweiSeekBar;
	Spinner searchNearTypeSpinner;
	
	ArrayList<User> userList;
	Button searchNearBtn;
	private ArrayAdapter adapter;
	 GeoPoint gp;
	Scenery bean;
	TextView fwValue;
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.search_near);
		
		Log.d(tag, "onCreate");
		Bundle bundle = this.getIntent().getExtras();
		bean=	bundle.getParcelable("bean");
		setTitle(bean.getSceneryName());
		gp=new GeoPoint((int) (new Double(bean.getLat().trim()) * 1E6), (int) (new Double(bean.getLon().trim()) * 1E6));
		init();

	}

	@Override
	protected void onPause() {
		BMapApiDemoApp app = (BMapApiDemoApp) this.getApplication();
		app.mBMapMan.stop();
		super.onPause();
	}

	@Override
	protected void onResume() {
		BMapApiDemoApp app = (BMapApiDemoApp) this.getApplication();
		app.mBMapMan.start();
		super.onResume();
	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}
	OnSeekBarChangeListener sbcl=new OnSeekBarChangeListener(){

		@Override
		public void onProgressChanged(SeekBar arg0, int pross, boolean arg2) {
			// TODO Auto-generated method stub
			Log.d(tag, "pross:"+pross);
			fwValue.setText(pross*100+"米");
		}

		@Override
		public void onStartTrackingTouch(SeekBar arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onStopTrackingTouch(SeekBar arg0) {
			// TODO Auto-generated method stub
			
		}
		
	};
	/****
	 * 界面初始化
	 * **/
	private void init() {
		Log.d(tag, "init");
		BMapApiDemoApp app = (BMapApiDemoApp) this.getApplication();
		if (app.mBMapMan == null) {
			app.mBMapMan = new BMapManager(getApplication());
			app.mBMapMan.init(app.mStrKey,
					new BMapApiDemoApp.MyGeneralListener());
		}
		app.mBMapMan.start();
		// 如果使用地图SDK，请初始化地图Activity
		super.initMapActivity(app.mBMapMan);

		mMapView = (MapView) findViewById(R.id.nearmapView);

		 fanweiSeekBar = (SeekBar) findViewById(R.id.fanweiSeekBar);
		 fanweiSeekBar.setOnSeekBarChangeListener(sbcl);
		 searchNearTypeSpinner = (Spinner) findViewById(R.id.searchNearTypeSpinner);
		 searchNearBtn = (Button) findViewById(R.id.searchNearBtn);
		adapter = ArrayAdapter.createFromResource(SearchNearActivity.this,
				R.array.searchTypes, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		searchNearTypeSpinner.setAdapter(adapter);
		fwValue=(TextView)findViewById(R.id.fwValue);
		mMapView.setBuiltInZoomControls(true);
		// 设置在缩放动画过程中也显示overlay,默认为不绘制
		mMapView.setDrawOverlayWhenZooming(true);

		// 初始化搜索模块，注册事件监听
		mSearch = new MKSearch();
		mSearch.init(app.mBMapMan, searchListner);

		// 设定搜索按钮的响应

		searchNearBtn.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				int mi=fanweiSeekBar.getProgress();
				mi=mi*100;
				String searchtype=(String) searchNearTypeSpinner.getSelectedItem();
				Log.d(tag, "mi:"+mi);
				Log.d(tag, "searchtype："+mi);
				
					searchNearButtonProcess(searchtype,gp,mi);
				
			}
		});

		mMapView.getController().animateTo(gp);

	}

	void searchNearButtonProcess(String key, GeoPoint gp, int mi) {

		Log.d(tag, "searchNearButtonProcess：key："+key+",mi:"+mi);

		mSearch.poiSearchNearBy(key, gp, mi);

	}

	ArrayList<MKPoiInfo> list = null;// 搜索结果信息
	MKSearchListener searchListner = new MKSearchListener() {
		public void onGetPoiResult(MKPoiResult res, int type, int error) {
			// 错误号可参考MKEvent中的定义
			Log.d(tag, "onGetPoiResult");
			if (error != 0 || res == null) {
				Toast.makeText(SearchNearActivity.this, "抱歉，未找到结果",
						Toast.LENGTH_LONG).show();
				return;
			}
			// 将地图移动到第一个POI中心点
			if (res.getCurrentNumPois() > 0) {

				list = res.getAllPoi();
				for (MKPoiInfo poi : list) {
					Log.d(tag,
							poi.name + "," + poi.city + ","
									+ poi.pt.getLatitudeE6() + ","
									+ poi.pt.getLongitudeE6());
				}
				// 将poi结果显示到地图上
				PoiOverlay poiOverlay = new PoiOverlay(SearchNearActivity.this,
						mMapView);
				poiOverlay.setData(res.getAllPoi());
				// mMapView.getOverlays().clear();
				mMapView.getOverlays().add(poiOverlay);
				mMapView.invalidate();
				mMapView.getController().animateTo(res.getPoi(0).pt);

				OnFocusChangeListener arg0 = new OnFocusChangeListener() {

					@Override
					public void onFocusChanged(ItemizedOverlay<?> arg0,
							OverlayItem arg1) {
						// TODO Auto-generated method stub
						Log.d(tag, arg0.size() + arg1.getTitle());
						if (arg0.getLastFocusedIndex() != -1) {
							int i = arg0.getLastFocusedIndex();
							final MKPoiInfo poi = list.get(i);
							Log.d(tag, i + ":" + list.get(i).name);
							AlertDialog dialog = new AlertDialog.Builder(
									SearchNearActivity.this).create();

							dialog.setMessage("名称:" + poi.name + "\n" + "地址："
									+ poi.address + "\n城市： " + poi.city + "坐标："
									+ poi.pt.getLatitudeE6() + ","
									+ poi.pt.getLongitudeE6());
							dialog.setButton("OK",
									new DialogInterface.OnClickListener() {

										@Override
										public void onClick(
												DialogInterface dialog,
												int which) {
											// TODO Auto-generated method stub
											dialog.dismiss();

										}
									});

							dialog.show();

						}
					}

				};
				poiOverlay.setOnFocusChangeListener(arg0);
			} else if (res.getCityListNum() > 0) {
				String strInfo = "在";
				for (int i = 0; i < res.getCityListNum(); i++) {
					strInfo += res.getCityListInfo(i).city;
					strInfo += ",";
				}
				strInfo += "找到结果";
				Toast.makeText(SearchNearActivity.this, strInfo,
						Toast.LENGTH_LONG).show();
			}
		}

		public void onGetDrivingRouteResult(MKDrivingRouteResult res, int error) {
			Log.d(tag, "onGetDrivingRouteResult");

		}

		public void onGetTransitRouteResult(MKTransitRouteResult res, int error) {

		}

		// 驾车
		public void onGetWalkingRouteResult(MKWalkingRouteResult res, int error) {

		}

		public void onGetAddrResult(MKAddrInfo res, int error) {

		}
	};

	private void getuserList() {
		// TODO Auto-generated method stub
		Log.d(tag, "getuserList");
		Bundle bundle = this.getIntent().getExtras();
		userList = bundle.getParcelableArrayList("list");
		Log.d(tag, "sceneryList.size():" + userList.size());
	}

}
